package art.client.activites;

import art.client.ClientFactory;
import art.client.ClientUtils;
import art.client.places.ArtUploadPlace;
import art.client.places.ArtsListPlace;
import art.client.places.LoginPlace;
import art.client.places.UserArtsPlace;
import art.client.places.UserInfoPlace;
import art.client.places.UsersListPlace;
import art.client.views.MenuView;
import art.shared.entities.LoginInfo;
import art.shared.entities.UserLevelShared;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class MenuHandler {

	private LoginInfo loginInfo = null;
	
	public MenuHandler(final ClientFactory factory, MenuView menu,
			LoginInfo loginInfo) {
		this.loginInfo = loginInfo;
		UserLevelShared level = UserLevelShared.VISITOR;
		if (loginInfo != null) {
			level = loginInfo.getUserLevel();
		}
		switch (level) {
		case VISITOR:
			createLoginMenu(factory, menu);
			createArtsMenu(factory, menu);
			break;
		case NORMAL:
			createArtsMenu(factory, menu);
			createUploadArtMenu(factory, menu);
			createOwnArtsMenu(factory, menu);
			createEditProfileMenu(factory, menu);
			createLogoutMenu(factory, menu);
			break;
		case ADMIN:
			createArtsMenu(factory, menu);
			createUploadArtMenu(factory, menu);
			createOwnArtsMenu(factory, menu);
			createEditProfileMenu(factory, menu);
			createUsersMenu(factory, menu);
			createLogoutMenu(factory, menu);
			break;
		}
	}

	private void createLoginMenu(final ClientFactory factory, MenuView menu) {
		menu.getAnchor("Login").addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent arg0) {
				factory.getPlaceController().goTo(new LoginPlace("login"));
			}
		});
	}

	private void createArtsMenu(final ClientFactory factory, MenuView menu) {
		menu.getAnchor("Arts").addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent arg0) {
				factory.getPlaceController().goTo(new ArtsListPlace("arts", loginInfo));
			}
		});
	}

	private void createUsersMenu(final ClientFactory factory, MenuView menu) {
		menu.getAnchor("Users").addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent arg0) {
				factory.getPlaceController().goTo(new UsersListPlace("users", loginInfo));
			}
		});
	}

	private void createEditProfileMenu(final ClientFactory factory,
			MenuView menu) {
		menu.getAnchor("Edit profile").addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent arg0) {
				factory.getPlaceController().goTo(
						new UserInfoPlace("edit-profile", loginInfo));
			}
		});
	}

	private void createLogoutMenu(final ClientFactory factory, MenuView menu) {
		menu.getAnchor("Logout").addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent arg0) {
				ClientUtils.getUserService().logout(new AsyncCallback<Boolean>() {

					@Override
					public void onSuccess(Boolean result) {
						if (result) {
							factory.getPlaceController().goTo(new LoginPlace("login"));
						} else {
							Window.alert("Error: Couldn't perform logout!");
						}
					}

					@Override
					public void onFailure(Throwable arg0) {
						Window.alert("Error: Couldn't perform logout!");
					}
				});
			}
		});
	}

	private void createUploadArtMenu(final ClientFactory factory, MenuView menu) {
		menu.getAnchor("Upload arts").addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent arg0) {
				factory.getPlaceController().goTo(
						new ArtUploadPlace("upload-art", loginInfo));
			}
		});
	}

	private void createOwnArtsMenu(final ClientFactory factory, MenuView menu) {
		menu.getAnchor("Own arts").addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent arg0) {
				factory.getPlaceController()
						.goTo(new UserArtsPlace("own-arts", loginInfo));
			}
		});
	}
}
